package cn.cloud.all.security.oauth2.provider.token.store;

import cn.cloud.all.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * A {@link JwtClaimsSetVerifier} that delegates claims verification
 * to it's internal <code>List</code> of {@link JwtClaimsSetVerifier}'s.
 */
public class DelegatingJwtClaimsSetVerifier implements JwtClaimsSetVerifier {

    private final List<JwtClaimsSetVerifier> jwtClaimsSetVerifiers;

    public DelegatingJwtClaimsSetVerifier(List<JwtClaimsSetVerifier> jwtClaimsSetVerifiers) {
        Assert.notEmpty(jwtClaimsSetVerifiers, "jwtClaimsSetVerifiers cannot be empty");
        this.jwtClaimsSetVerifiers = Collections.unmodifiableList(new ArrayList<>(jwtClaimsSetVerifiers));
    }

    @Override
    public void verify(Map<String, Object> claims) throws InvalidTokenException {
        for (JwtClaimsSetVerifier jwtClaimsSetVerifier : this.jwtClaimsSetVerifiers) {
            jwtClaimsSetVerifier.verify(claims);
        }
    }
}